package sfeir.gwt.ergosoom.server.service.rest;

import java.util.logging.Logger;

import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import sfeir.gwt.ergosoom.client.model.Profile;
import sfeir.gwt.ergosoom.server.service.ProfileAccess;

import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ProfileService extends ServerResource {

    private static final Logger logger = Logger.getLogger(ProfileService.class.getName());

    @Get
    public Representation fetch() {
        String alias = (String) getRequest().getAttributes().get("alias");
        String passwd = (String) getRequest().getAttributes().get("passwd");
        Profile p = new ProfileAccess().get(alias);
        if (null == p) {
            MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
            if (null != cache.get(alias))
                setStatus(new Status(Status.CLIENT_ERROR_BAD_REQUEST, "Unconfirmed"), "Unconfirmed");
            else
                setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, "Nonexistent"), "Nonexistent");
            return null;
        }
        if (!passwd.equals(p.getPassword())) {
            setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Bad password");
            return null;
        }
        XStream xstream = new XStream(new PureJavaReflectionProvider(), new DomDriver());
        xstream.processAnnotations(Profile.class);
//        xstream.alias("profile", Profile.class);
//        logger.info("before marshal");
        String repr = xstream.toXML(p);
//        logger.info("after marshal : " + repr);
        return new StringRepresentation(repr);
    }

    @Put
    public String save(String repr) {
//        logger.info(repr);
        ProfileAccess pa = new ProfileAccess();
        String passwd = (String) getRequest().getAttributes().get("passwd");
        Profile profile;
        try {
//            logger.info("xstream init");
            XStream xstream = new XStream(new PureJavaReflectionProvider(), new DomDriver());
            xstream.processAnnotations(Profile.class);
//            logger.info("before unmarshal");
            Object o = xstream.fromXML(repr);
//            logger.info("after unmarshal : " + o);
            profile = (Profile) o;
//            logger.info("after unmarshal : " + profile);
            String profPasswd = pa.getPassword(profile.getAlias());
            if (!passwd.equals(profPasswd)) {
                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Bad password");
                return null;
            }
            pa.save(profile);
        } catch (IllegalArgumentException e) {
            logger.severe(e.toString());
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
            return "KO";
        } catch (Exception e) {
            logger.severe(e.toString());
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage());
            return "KO";
        }
        return "OK";
    }
}
